//
//  C205DisCoverViewController.m
//  云音乐
//
//  Created by lanou3g on 15/6/3.
//  Copyright (c) 2015年 C205. All rights reserved.
//
#define kMainScreenWidth [[UIScreen mainScreen]bounds].size.width//屏幕宽度

#define kMainScreenHeight [[UIScreen mainScreen]bounds].size.height//屏幕高度

#import "C205DisCoverViewController.h"
#import "VoiceSearchViewController.h"
#import "C205PlayingMusicViewController.h"
#import "C205PlayingMusicItemAnimate.h"
#import "C205SearchResultsTableViewController.h"

@interface C205DisCoverViewController ()<UISearchResultsUpdating,UISearchBarDelegate>
{
    UISearchController * _searchController;
}
@end

@implementation C205DisCoverViewController

#pragma mark - 生命周期方法
#pragma mark -
- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    [self loadNaviBarItem];
    [self loadSearch];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Navigation
#pragma mark -
/**
 *  自定义navigationItem
 */
- (void)loadNaviBarItem
{
    /**
     *  左侧按钮
     */
    UIButton *voidSearchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    voidSearchButton.frame = CGRectMake(0, 0, 24, 24);
    [voidSearchButton setBackgroundImage:[UIImage imageNamed:@"cm2_top_icn_mic"] forState:UIControlStateNormal];
    [voidSearchButton setBackgroundImage:[UIImage imageNamed:@"cm2_top_icn_mic_prs"] forState:UIControlStateHighlighted];
    [voidSearchButton addTarget:self action:@selector(cickForPushToVoiceVC) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:voidSearchButton];
    
    self.navigationItem.leftBarButtonItem = leftItem;
    
    /**
     右侧按钮
     */
    C205PlayingMusicItemAnimate * playingMusicItem = [[C205PlayingMusicItemAnimate alloc]init];
    [playingMusicItem addTarget:self action:@selector(cickForPushToMusicPlayingVc) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:playingMusicItem];
}
/**
 *  加载searchBar
 */
- (void)loadSearch
{
//    C205SearchResultsTableViewController  * results = [[C205SearchResultsTableViewController  alloc]init];
    _searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
    
    _searchController.searchResultsUpdater = self;
    
    _searchController.dimsBackgroundDuringPresentation = NO;
    
    _searchController.hidesNavigationBarDuringPresentation = NO;

    _searchController.searchBar.frame = CGRectMake(0, 0, 10, 44);
    
    _searchController.searchBar.placeholder = @"搜索音乐、歌词、电台";
    
    _searchController.searchBar.delegate  = self;
    
    _searchController.searchBar.backgroundColor = [UIColor clearColor];
    
    _searchController.searchBar.tintColor = [UIColor whiteColor];
    
    [self.navigationItem setTitleView:_searchController.searchBar];
}
/**
 *  点击左侧item响应事件
 */
- (void)cickForPushToVoiceVC
{
    VoiceSearchViewController * voice = [[VoiceSearchViewController alloc]init];
    [self.navigationController pushViewController:voice animated:YES];
    
}
/**
 *  点击右侧item响应事件
 */
- (void)cickForPushToMusicPlayingVc
{
    NSLog(@"跳转");
    C205PlayingMusicViewController * playingMusic = [[C205PlayingMusicViewController alloc]init];
    [self.navigationController pushViewController:playingMusic animated:YES];
}

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    
}


#pragma mark - searchBarDelegate
#pragma mark -
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    //    [self.navigationController.navigationItem setHidesBackButton:YES animated:YES];
    /**
     *  方案1  的确隐藏了 但是searchbar的尺寸没有发生变化
     */
    self.navigationItem.rightBarButtonItem.customView.hidden=YES;
    
    self.navigationItem.leftBarButtonItem.customView.hidden=YES;
    
    return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    self.navigationItem.rightBarButtonItem.customView.hidden=NO;
    self.navigationItem.leftBarButtonItem.customView.hidden=NO;
    
    return YES;
}
@end
